﻿
// GraphicView.cpp: CGraphicView 类的实现
//

#include "pch.h"
#include "framework.h"
// SHARED_HANDLERS 可以在实现预览、缩略图和搜索筛选器句柄的
// ATL 项目中进行定义，并允许与该项目共享文档代码。
#ifndef SHARED_HANDLERS
#include "Graphic.h"
#endif

#include "GraphicDoc.h"
#include "GraphicView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CGraphicView

IMPLEMENT_DYNCREATE(CGraphicView, CScrollView)

BEGIN_MESSAGE_MAP(CGraphicView, CScrollView)
	// 标准打印命令
	ON_COMMAND(ID_FILE_PRINT, &CScrollView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, &CScrollView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CScrollView::OnFilePrintPreview)
	ON_COMMAND(IDM_DOT, &CGraphicView::OnDot)
	ON_COMMAND(IDM_LINE, &CGraphicView::OnLine)
	ON_COMMAND(IDM_RECTANGLE, &CGraphicView::OnRectangle)
	ON_COMMAND(IDM_ELLIPSE, &CGraphicView::OnEllipse)
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_PAINT()
	ON_COMMAND(ID_FILE_SAVE, &CGraphicView::OnFileSave)
	ON_COMMAND(ID_FILE_OPEN, &CGraphicView::OnFileOpen)
END_MESSAGE_MAP()

// CGraphicView 构造/析构

CGraphicView::CGraphicView() noexcept
{
	// TODO: 在此处添加构造代码
	m_DrawType = 0;
	m_ptOrigin = 0;
	m_dcMetaFile.Create(NULL);// 内存源文件
}

CGraphicView::~CGraphicView()
{
	for (int i = 0; i < m_ptrArray.GetSize(); i++)
	{
		delete (CGraph*)m_ptrArray.GetAt(i);
	}
}

BOOL CGraphicView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: 在此处通过修改
	//  CREATESTRUCT cs 来修改窗口类或样式

	return CScrollView::PreCreateWindow(cs);
}

// CGraphicView 绘图

void CGraphicView::OnDraw(CDC* pDC)
{
	CGraphicDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if (!pDoc)
		return;

	// TODO: 在此处为本机数据添加绘制代码
	/*
	CBrush* pBrush = CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));

	CBrush* pOldBrush = pDC->SelectObject(pBrush);
	for (int i = 0; i < m_ptrArray.GetSize(); i++)
	{
		switch (((CGraph*)m_ptrArray.GetAt(i))->m_nDrawType)
		{
		case 1:
		{
			pDC->SetPixel(((CGraph*)m_ptrArray.GetAt(i))->m_ptEnd, RGB(0, 0, 0));
			break;
		}
		case 2:
		{
			pDC->MoveTo(((CGraph*)m_ptrArray.GetAt(i))->m_ptOrigin);
			pDC->LineTo(((CGraph*)m_ptrArray.GetAt(i))->m_ptEnd);
			break;
		}
		case 3:
		{
			pDC->Rectangle(CRect(((CGraph*)m_ptrArray.GetAt(i))->m_ptOrigin, ((CGraph*)m_ptrArray.GetAt(i))->m_ptEnd));
			break;
		}
		case 4:
		{
			pDC->Ellipse(CRect(((CGraph*)m_ptrArray.GetAt(i))->m_ptOrigin, ((CGraph*)m_ptrArray.GetAt(i))->m_ptEnd));
			break;
		}
		default:
			break;
		}
	}
	pDC->SelectObject(pOldBrush);
	*/

	/*
	// 在内存中保存源文件

	HMETAFILE hMetaFile;
	hMetaFile = m_dcMetaFile.Close();// 源文件句柄
	pDC->PlayMetaFile(hMetaFile); // 在窗口输出
	m_dcMetaFile.Create();// 创建源文件
	m_dcMetaFile.PlayMetaFile(hMetaFile); // 在新的源文件中绘制
	DeleteMetaFile(hMetaFile);
	*/

	// 兼容dc拷贝到目的dc保存文件
	CRect rect;
	GetClientRect(&rect);
	pDC->BitBlt(0, 0, rect.Width(), rect.Height(), &m_dcCompatible, 0, 0, SRCCOPY);
}


// CGraphicView 打印

BOOL CGraphicView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// 默认准备
	return DoPreparePrinting(pInfo);
}

void CGraphicView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: 添加额外的打印前进行的初始化过程
}

void CGraphicView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: 添加打印后进行的清理过程
}


// CGraphicView 诊断

#ifdef _DEBUG
void CGraphicView::AssertValid() const
{
	CScrollView::AssertValid();
}

void CGraphicView::Dump(CDumpContext& dc) const
{
	CScrollView::Dump(dc);
}

CGraphicDoc* CGraphicView::GetDocument() const // 非调试版本是内联的
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CGraphicDoc)));
	return (CGraphicDoc*)m_pDocument;
}
#endif //_DEBUG


// CGraphicView 消息处理程序


void CGraphicView::OnDot()
{
	// TODO: 在此添加命令处理程序代码
	m_DrawType = 1;
}


void CGraphicView::OnLine()
{
	// TODO: 在此添加命令处理程序代码
	m_DrawType = 2;

}


void CGraphicView::OnRectangle()
{
	// TODO: 在此添加命令处理程序代码
	m_DrawType = 3;

}


void CGraphicView::OnEllipse()
{
	// TODO: 在此添加命令处理程序代码
	m_DrawType = 4;

}


void CGraphicView::OnLButtonDown(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	m_ptOrigin = point;
	CScrollView::OnLButtonDown(nFlags, point);
}


void CGraphicView::OnLButtonUp(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	CClientDC dc(this);
	CBrush* pBrush = CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));

	//dc.SelectObject(pBrush);
	m_dcMetaFile.SelectObject(pBrush);

	if (!m_dcCompatible.m_hDC) {
		m_dcCompatible.CreateCompatibleDC(&dc);// 兼容dc
		CRect rect;
		GetClientRect(&rect);
		CBitmap bitmap;
		bitmap.CreateCompatibleBitmap(&dc, rect.Width(), rect.Height());
		m_dcCompatible.SelectObject(&bitmap);
		m_dcCompatible.BitBlt(0, 0, rect.Width(), rect.Height(), &dc, 0, 0, SRCCOPY);
		m_dcCompatible.SelectObject(pBrush);
	}

	switch (m_DrawType)
	{
	case 1:
	{
		//dc.SetPixel(point, RGB(0, 0, 0));
		//m_dcMetaFile.SetPixel(point, RGB(0, 0, 0));
		m_dcCompatible.SetPixel(point, RGB(0, 0, 0));
		break;
	}
	case 2:
	{
		//m_dcMetaFile.MoveTo(m_ptOrigin);
		//m_dcMetaFile.LineTo(point);
		m_dcCompatible.MoveTo(m_ptOrigin);
		m_dcCompatible.LineTo(point);

		break;
	}
	case 3:
	{
		//m_dcMetaFile.Rectangle(CRect(m_ptOrigin, point));
		m_dcCompatible.Rectangle(CRect(m_ptOrigin, point));
		break;
	}
	case 4:
	{
		//m_dcMetaFile.Ellipse(CRect(m_ptOrigin, point));
		m_dcCompatible.Ellipse(CRect(m_ptOrigin, point));
		break;
	}
	default:
		break;
	}

	/*
	OnPrepareDC(&dc);
	dc.DPtoLP(&m_ptOrigin); // 坐标变换
	dc.DPtoLP(&point);

	CGraph* graph = new CGraph(m_DrawType, m_ptOrigin, point);
	m_ptrArray.Add(graph);
	*/
	CScrollView::OnLButtonUp(nFlags, point);
}


void CGraphicView::OnPaint()
{
	CPaintDC dc(this); // device context for painting
					   // TODO: 在此处添加消息处理程序代码
					   // 不为绘图消息调用 CScrollView::OnPaint()
	OnPrepareDC(&dc);
	OnDraw(&dc);
}


void CGraphicView::OnInitialUpdate()
{
	CScrollView::OnInitialUpdate();

	// TODO: 在此添加专用代码和/或调用基类
	SetScrollSizes(MM_TEXT, CSize(800, 600));
}


void CGraphicView::OnFileSave()
{
	// TODO: 在此添加命令处理程序代码
	HMETAFILE hMetaFile;
	hMetaFile = m_dcMetaFile.Close();
	CopyMetaFile(hMetaFile, "meta.wmf");

	m_dcMetaFile.Create();
	DeleteMetaFile(hMetaFile);
}


void CGraphicView::OnFileOpen()
{
	// TODO: 在此添加命令处理程序代码
	HMETAFILE hMetaFile;
	hMetaFile = GetMetaFile("meta.wmf");
	m_dcMetaFile.PlayMetaFile(hMetaFile);// 源文件中播放
	DeleteMetaFile(hMetaFile);
	Invalidate();
}
